/*
  ==============================================================================

  This is an automatically generated file created by the Jucer!

  Creation date:  14 Sep 2011 10:45:59am

  Be careful when adding custom code to these files, as only the code within
  the "//[xyz]" and "//[/xyz]" sections will be retained when the file is loaded
  and re-saved.

  Jucer version: 1.12

  ------------------------------------------------------------------------------

  The Jucer is part of the JUCE library - "Jules' Utility Class Extensions"
  Copyright 2004-6 by Raw Material Software ltd.

  ==============================================================================
*/

//[Headers] You can add your own extra header files here...
//[/Headers]

#include "curvegui.h"


//[MiscUserDefs] You can add your own user definitions and misc code here...
//[/MiscUserDefs]

//==============================================================================
CurveEditor::CurveEditor (MidiCurve* const ownerFilter)
    : AudioProcessorEditor (ownerFilter),
      curve (0),
      label (0),
      label2 (0),
      resizer (0),
      channelSlider (0),
      label3 (0),
      resetButton (0),
      helpButton (0),
      instructionsLabel (0),
      label4 (0),
      rangeSlider (0),
      label5 (0),
      label6 (0),
      label7 (0),
      rangeSlider2 (0),
      label8 (0)
{
    addAndMakeVisible (curve = new MidiEnvelope (0,this,this->getFilter()));
    curve->setName (L"curve");

    addAndMakeVisible (label = new Label (L"new label",
                                          L"In: --"));
    label->setFont (Font (15.0000f, Font::plain));
    label->setJustificationType (Justification::centredLeft);
    label->setEditable (false, false, false);
    label->setColour (TextEditor::textColourId, Colours::black);
    label->setColour (TextEditor::backgroundColourId, Colour (0x0));

    addAndMakeVisible (label2 = new Label (L"new label",
                                           L"Out: --"));
    label2->setFont (Font (15.0000f, Font::plain));
    label2->setJustificationType (Justification::centredLeft);
    label2->setEditable (false, false, false);
    label2->setColour (TextEditor::textColourId, Colours::black);
    label2->setColour (TextEditor::backgroundColourId, Colour (0x0));

    addAndMakeVisible (resizer = new ResizableCornerComponent (this, &resizeLimits));

    addAndMakeVisible (channelSlider = new ChannelSlider (L"new slider"));
    channelSlider->setRange (0, 16, 1);
    channelSlider->setSliderStyle (Slider::IncDecButtons);
    channelSlider->setTextBoxStyle (Slider::TextBoxLeft, false, 35, 20);
    channelSlider->setColour (Slider::textBoxTextColourId, Colours::black);
    channelSlider->addListener (this);

    addAndMakeVisible (label3 = new Label (L"new label",
                                           L"Channel"));
    label3->setFont (Font (15.0000f, Font::plain));
    label3->setJustificationType (Justification::centredLeft);
    label3->setEditable (false, false, false);
    label3->setColour (TextEditor::textColourId, Colours::black);
    label3->setColour (TextEditor::backgroundColourId, Colour (0x0));

    addAndMakeVisible (resetButton = new TextButton (L"new button"));
    resetButton->setButtonText (L"Reset");
    resetButton->addListener (this);

    addAndMakeVisible (helpButton = new TextButton (L"new button"));
    helpButton->setButtonText (L"Help");
    helpButton->addListener (this);

    addAndMakeVisible (instructionsLabel = new Label (L"new label",
                                                      L"X is input, Y is output.\nMax 32 points.\n\nDefault settings load from \"midiPBCurve.fxb\"\n_______________\n\nDouble-click or Right-click: \nadd or delete a point\n\nMiddle-click or Alt-click: \nmake a curve control point\n\nShift-click/drag: \nset point to center line\n\nCtrl-Shift-click/drag: \nset point to Y-center\n\nCtrl-drag: \nrestrict to horizontal/vertical movement"));
    instructionsLabel->setFont (Font (18.0000f, Font::plain));
    instructionsLabel->setJustificationType (Justification::centred);
    instructionsLabel->setEditable (false, false, false);
    instructionsLabel->setColour (Label::backgroundColourId, Colour (0xcbffffff));
    instructionsLabel->setColour (TextEditor::textColourId, Colours::black);
    instructionsLabel->setColour (TextEditor::backgroundColourId, Colour (0x0));

    addAndMakeVisible (label4 = new Label (L"new label",
                                           L"Last Message"));
    label4->setFont (Font (15.0000f, Font::bold));
    label4->setJustificationType (Justification::centred);
    label4->setEditable (false, false, false);
    label4->setColour (TextEditor::textColourId, Colours::black);
    label4->setColour (TextEditor::backgroundColourId, Colour (0x0));

    addAndMakeVisible (rangeSlider = new Slider (L"new slider"));
    rangeSlider->setRange (0, 24, 1);
    rangeSlider->setSliderStyle (Slider::IncDecButtons);
    rangeSlider->setTextBoxStyle (Slider::TextBoxLeft, false, 35, 20);
    rangeSlider->setColour (Slider::textBoxTextColourId, Colours::black);
    rangeSlider->addListener (this);

    addAndMakeVisible (label5 = new Label (L"new label",
                                           L"PB Range +"));
    label5->setFont (Font (15.0000f, Font::plain));
    label5->setJustificationType (Justification::centredLeft);
    label5->setEditable (false, false, false);
    label5->setColour (TextEditor::textColourId, Colours::black);
    label5->setColour (TextEditor::backgroundColourId, Colour (0x0));

    addAndMakeVisible (label6 = new Label (L"new label",
                                           String::empty));
    label6->setFont (Font (15.0000f, Font::plain));
    label6->setJustificationType (Justification::centredLeft);
    label6->setEditable (false, false, false);
    label6->setColour (TextEditor::textColourId, Colours::black);
    label6->setColour (TextEditor::backgroundColourId, Colour (0x0));

    addAndMakeVisible (label7 = new Label (L"new label",
                                           String::empty));
    label7->setFont (Font (15.0000f, Font::plain));
    label7->setJustificationType (Justification::centredLeft);
    label7->setEditable (false, false, false);
    label7->setColour (TextEditor::textColourId, Colours::black);
    label7->setColour (TextEditor::backgroundColourId, Colour (0x0));

    addAndMakeVisible (rangeSlider2 = new Slider (L"new slider"));
    rangeSlider2->setRange (0, 24, 1);
    rangeSlider2->setSliderStyle (Slider::IncDecButtons);
    rangeSlider2->setTextBoxStyle (Slider::TextBoxLeft, false, 35, 20);
    rangeSlider2->setColour (Slider::textBoxTextColourId, Colours::black);
    rangeSlider2->addListener (this);

    addAndMakeVisible (label8 = new Label (L"new label",
                                           L"PB Range -"));
    label8->setFont (Font (15.0000f, Font::plain));
    label8->setJustificationType (Justification::centredLeft);
    label8->setEditable (false, false, false);
    label8->setColour (TextEditor::textColourId, Colours::black);
    label8->setColour (TextEditor::backgroundColourId, Colour (0x0));


    //[UserPreSize]
	static PizLookAndFeel Look;
	LookAndFeel::setDefaultLookAndFeel (&Look);

	resizeLimits.setSizeLimits (50, 50, 1600, 1600);
	instructionsLabel->setVisible(false);
    //[/UserPreSize]

    setSize (600, 400);


    //[Constructor] You can add your own custom stuff here..
	ownerFilter->addChangeListener (this);
	this->updateParameters();
    //[/Constructor]
}

CurveEditor::~CurveEditor()
{
    //[Destructor_pre]. You can add your own custom destruction code here..
	getFilter()->removeChangeListener (this);
    //[/Destructor_pre]

    deleteAndZero (curve);
    deleteAndZero (label);
    deleteAndZero (label2);
    deleteAndZero (resizer);
    deleteAndZero (channelSlider);
    deleteAndZero (label3);
    deleteAndZero (resetButton);
    deleteAndZero (helpButton);
    deleteAndZero (instructionsLabel);
    deleteAndZero (label4);
    deleteAndZero (rangeSlider);
    deleteAndZero (label5);
    deleteAndZero (label6);
    deleteAndZero (label7);
    deleteAndZero (rangeSlider2);
    deleteAndZero (label8);


    //[Destructor]. You can add your own custom destruction code here..
    //[/Destructor]
}

//==============================================================================
void CurveEditor::paint (Graphics& g)
{
    //[UserPrePaint] Add your own custom painting code here..
    //[/UserPrePaint]

    g.fillAll (Colour (0xffdddddd));

    //[UserPaint] Add your own custom painting code here..
    //[/UserPaint]
}

void CurveEditor::resized()
{
    curve->setBounds (8, 8, getWidth() - 93, getHeight() - 16);
    label->setBounds (getWidth() - 6 - 67, 61, 67, 16);
    label2->setBounds (getWidth() - 8 - 72, 98, 72, 16);
    resizer->setBounds (getWidth() - 16, getHeight() - 16, 16, 16);
    channelSlider->setBounds (getWidth() - 6 - 72, 148, 72, 16);
    label3->setBounds (getWidth() - 11 - 72, 132, 72, 16);
    resetButton->setBounds (getWidth() - 6 - 72, 261, 72, 24);
    helpButton->setBounds (getWidth() - 6 - 72, 293, 72, 24);
    instructionsLabel->setBounds (16, 16, getWidth() - 108, getHeight() - 32);
    label4->setBounds (getWidth() - 6 - 72, 16, 72, 45);
    rangeSlider->setBounds (getWidth() - 6 - 72, 197, 72, 16);
    label5->setBounds (getWidth() - -3 - 86, 181, 86, 16);
    label6->setBounds (getWidth() - 12 - 51, 77, 51, 16);
    label7->setBounds (getWidth() - 12 - 51, 114, 51, 16);
    rangeSlider2->setBounds (getWidth() - 6 - 72, 232, 72, 16);
    label8->setBounds (getWidth() - -3 - 86, 216, 86, 16);
    //[UserResized] Add your own custom resize handling here..
	getFilter()->lastUIHeight=getHeight();
	getFilter()->lastUIWidth=getWidth();
    //[/UserResized]
}

void CurveEditor::sliderValueChanged (Slider* sliderThatWasMoved)
{
    //[UsersliderValueChanged_Pre]
    //[/UsersliderValueChanged_Pre]

    if (sliderThatWasMoved == channelSlider)
    {
        //[UserSliderCode_channelSlider] -- add your slider handling code here..
		getFilter()->setParameterNotifyingHost(kChannel,(float)(channelSlider->getValue()/channelSlider->getMaximum()));
        //[/UserSliderCode_channelSlider]
    }
    else if (sliderThatWasMoved == rangeSlider)
    {
        //[UserSliderCode_rangeSlider] -- add your slider handling code here..
		getFilter()->setParameterNotifyingHost(kPBRange,(float)(rangeSlider->getValue()/rangeSlider->getMaximum()));
        //[/UserSliderCode_rangeSlider]
    }
    else if (sliderThatWasMoved == rangeSlider2)
    {
        //[UserSliderCode_rangeSlider2] -- add your slider handling code here..
		getFilter()->setParameterNotifyingHost(kPBRange2,(float)(rangeSlider2->getValue()/rangeSlider2->getMaximum()));
        //[/UserSliderCode_rangeSlider2]
    }

    //[UsersliderValueChanged_Post]
    //[/UsersliderValueChanged_Post]
}

void CurveEditor::buttonClicked (Button* buttonThatWasClicked)
{
    //[UserbuttonClicked_Pre]
    //[/UserbuttonClicked_Pre]

    if (buttonThatWasClicked == resetButton)
    {
        //[UserButtonCode_resetButton] -- add your button handler code here..
		getFilter()->resetPoints();
        //[/UserButtonCode_resetButton]
    }
    else if (buttonThatWasClicked == helpButton)
    {
        //[UserButtonCode_helpButton] -- add your button handler code here..
		instructionsLabel->setVisible(!instructionsLabel->isVisible());
        //[/UserButtonCode_helpButton]
    }

    //[UserbuttonClicked_Post]
    //[/UserbuttonClicked_Post]
}



//[MiscUserCode] You can add your own definitions of your custom methods or any other code here...
void CurveEditor::changeListenerCallback (ChangeBroadcaster* source)
{
	if(source==getFilter())
		updateParameters();
}

void CurveEditor::updateParameters ()
{
	const int lastin = getFilter()->lastCCIn;
	const int lastout = getFilter()->lastCCOut;
	const float pbrange = getFilter()->getParameter(kPBRange)*48.f;
	const float pbrange2 = getFilter()->getParameter(kPBRange2)*48.f;
	if (lastin!=-1) {
		label->setText("In: " + String(lastin),true);
		if (lastin==8192) label6->setText("(center)",true);
		else if (lastin>8192) label6->setText("(+"+String(pbrange*((float)lastin/16383.f-0.5f),2) + ")",true);
		else if (lastin<8192) label6->setText("("+String(pbrange2*((float)lastin/16383.f-0.5f),2) + ")",true);
	}
	if (lastout!=-1) {
		label2->setText("Out: " + String(lastout),true);
		if (lastout==8192) label7->setText("(center)",true);
		else if (lastout>8192) label7->setText("(+"+String(pbrange*((float)lastout/16383.f-0.5f),2) + ")",true);
		else if (lastout<8192) label7->setText("("+String(pbrange2*((float)lastout/16383.f-0.5f),2) + ")",true);
	}

	channelSlider->setValue(getFilter()->getParameter(kChannel)*channelSlider->getMaximum(),false);
	rangeSlider->setValue(getFilter()->getParameter(kPBRange)*rangeSlider->getMaximum(),false);
	rangeSlider2->setValue(getFilter()->getParameter(kPBRange2)*rangeSlider2->getMaximum(),false);

	curve->updateParameters(true);

	setSize(getFilter()->lastUIWidth,getFilter()->lastUIHeight);
}
//[/MiscUserCode]


//==============================================================================
#if 0
/*  -- Jucer information section --

    This is where the Jucer puts all of its metadata, so don't change anything in here!

BEGIN_JUCER_METADATA

<JUCER_COMPONENT documentType="Component" className="CurveEditor" componentName=""
                 parentClasses="public AudioProcessorEditor, public ChangeListener"
                 constructorParams="MidiCurve* const ownerFilter" variableInitialisers="AudioProcessorEditor (ownerFilter)"
                 snapPixels="8" snapActive="1" snapShown="1" overlayOpacity="0.330000013"
                 fixedSize="0" initialWidth="600" initialHeight="400">
  <BACKGROUND backgroundColour="ffdddddd"/>
  <GENERICCOMPONENT name="curve" id="a93377748ddb46bb" memberName="curve" virtualName=""
                    explicitFocusOrder="0" pos="8 8 93M 16M" class="MidiEnvelope"
                    params="0,this,this-&gt;getFilter()"/>
  <LABEL name="new label" id="2453dab56ac8437b" memberName="label" virtualName=""
         explicitFocusOrder="0" pos="6Rr 61 67 16" edTextCol="ff000000"
         edBkgCol="0" labelText="In: --" editableSingleClick="0" editableDoubleClick="0"
         focusDiscardsChanges="0" fontname="Default font" fontsize="15"
         bold="0" italic="0" justification="33"/>
  <LABEL name="new label" id="f7832c70777477b2" memberName="label2" virtualName=""
         explicitFocusOrder="0" pos="8Rr 98 72 16" edTextCol="ff000000"
         edBkgCol="0" labelText="Out: --" editableSingleClick="0" editableDoubleClick="0"
         focusDiscardsChanges="0" fontname="Default font" fontsize="15"
         bold="0" italic="0" justification="33"/>
  <GENERICCOMPONENT name="" id="2a32bbdca363149e" memberName="resizer" virtualName=""
                    explicitFocusOrder="0" pos="0Rr 0Rr 16 16" class="ResizableCornerComponent"
                    params="this, &amp;resizeLimits"/>
  <SLIDER name="new slider" id="a4e096bdcf717868" memberName="channelSlider"
          virtualName="ChannelSlider" explicitFocusOrder="0" pos="6Rr 148 72 16"
          textboxtext="ff000000" min="0" max="16" int="1" style="IncDecButtons"
          textBoxPos="TextBoxLeft" textBoxEditable="1" textBoxWidth="35"
          textBoxHeight="20" skewFactor="1"/>
  <LABEL name="new label" id="59be378b1bb0a393" memberName="label3" virtualName=""
         explicitFocusOrder="0" pos="11Rr 132 72 16" edTextCol="ff000000"
         edBkgCol="0" labelText="Channel" editableSingleClick="0" editableDoubleClick="0"
         focusDiscardsChanges="0" fontname="Default font" fontsize="15"
         bold="0" italic="0" justification="33"/>
  <TEXTBUTTON name="new button" id="d5b7ebe4f17e4a55" memberName="resetButton"
              virtualName="" explicitFocusOrder="0" pos="6Rr 261 72 24" buttonText="Reset"
              connectedEdges="0" needsCallback="1" radioGroupId="0"/>
  <TEXTBUTTON name="new button" id="619bb49c64605970" memberName="helpButton"
              virtualName="" explicitFocusOrder="0" pos="6Rr 293 72 24" buttonText="Help"
              connectedEdges="0" needsCallback="1" radioGroupId="0"/>
  <LABEL name="new label" id="8dd803ff153c9de5" memberName="instructionsLabel"
         virtualName="" explicitFocusOrder="0" pos="16 16 108M 32M" bkgCol="cbffffff"
         edTextCol="ff000000" edBkgCol="0" labelText="X is input, Y is output.&#10;Max 32 points.&#10;&#10;Default settings load from &quot;midiPBCurve.fxb&quot;&#10;_______________&#10;&#10;Double-click or Right-click: &#10;add or delete a point&#10;&#10;Middle-click or Alt-click: &#10;make a curve control point&#10;&#10;Shift-click/drag: &#10;set point to center line&#10;&#10;Ctrl-Shift-click/drag: &#10;set point to Y-center&#10;&#10;Ctrl-drag: &#10;restrict to horizontal/vertical movement"
         editableSingleClick="0" editableDoubleClick="0" focusDiscardsChanges="0"
         fontname="Default font" fontsize="18" bold="0" italic="0" justification="36"/>
  <LABEL name="new label" id="76fe7ddb2ab9df1" memberName="label4" virtualName=""
         explicitFocusOrder="0" pos="6Rr 16 72 45" edTextCol="ff000000"
         edBkgCol="0" labelText="Last Message" editableSingleClick="0"
         editableDoubleClick="0" focusDiscardsChanges="0" fontname="Default font"
         fontsize="15" bold="1" italic="0" justification="36"/>
  <SLIDER name="new slider" id="eda942c14ab3c70e" memberName="rangeSlider"
          virtualName="" explicitFocusOrder="0" pos="6Rr 197 72 16" textboxtext="ff000000"
          min="0" max="24" int="1" style="IncDecButtons" textBoxPos="TextBoxLeft"
          textBoxEditable="1" textBoxWidth="35" textBoxHeight="20" skewFactor="1"/>
  <LABEL name="new label" id="4790fe757cf3918a" memberName="label5" virtualName=""
         explicitFocusOrder="0" pos="-3Rr 181 86 16" edTextCol="ff000000"
         edBkgCol="0" labelText="PB Range +" editableSingleClick="0" editableDoubleClick="0"
         focusDiscardsChanges="0" fontname="Default font" fontsize="15"
         bold="0" italic="0" justification="33"/>
  <LABEL name="new label" id="68ffc50c40b39786" memberName="label6" virtualName=""
         explicitFocusOrder="0" pos="12Rr 77 51 16" edTextCol="ff000000"
         edBkgCol="0" labelText="" editableSingleClick="0" editableDoubleClick="0"
         focusDiscardsChanges="0" fontname="Default font" fontsize="15"
         bold="0" italic="0" justification="33"/>
  <LABEL name="new label" id="5a28bf0087037467" memberName="label7" virtualName=""
         explicitFocusOrder="0" pos="12Rr 114 51 16" edTextCol="ff000000"
         edBkgCol="0" labelText="" editableSingleClick="0" editableDoubleClick="0"
         focusDiscardsChanges="0" fontname="Default font" fontsize="15"
         bold="0" italic="0" justification="33"/>
  <SLIDER name="new slider" id="b296a671e0d8ba04" memberName="rangeSlider2"
          virtualName="" explicitFocusOrder="0" pos="6Rr 232 72 16" textboxtext="ff000000"
          min="0" max="24" int="1" style="IncDecButtons" textBoxPos="TextBoxLeft"
          textBoxEditable="1" textBoxWidth="35" textBoxHeight="20" skewFactor="1"/>
  <LABEL name="new label" id="5f6c75f935fce83" memberName="label8" virtualName=""
         explicitFocusOrder="0" pos="-3Rr 216 86 16" edTextCol="ff000000"
         edBkgCol="0" labelText="PB Range -" editableSingleClick="0" editableDoubleClick="0"
         focusDiscardsChanges="0" fontname="Default font" fontsize="15"
         bold="0" italic="0" justification="33"/>
</JUCER_COMPONENT>

END_JUCER_METADATA
*/
#endif
